from django.db import models
from ckeditor_uploader.fields import RichTextUploadingField
from taggit_autosuggest.managers import TaggableManager
from django.urls import reverse

# Create your models here.

class BlogIndex(models.Model):
    pass


class BlogColumn(models.Model):
    """
        文章栏目的 Model
        """
    # 栏目标题
    title = models.CharField(max_length=100, blank=True)

    # 创建时间
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    def __str__(self):
        return self.title

class BlogPage(models.Model):

    """ 权限 """
    Is_Public_CHOICES = (
        (1, "公开可见"),
        (2, "仅我可见"),
        (3, "好友可见"),
    )

    title = models.CharField(verbose_name='标题', max_length=50)
    intro = models.TextField(verbose_name='简介', max_length=150)
    column = models.ForeignKey(BlogColumn, null=True, blank=True, on_delete=models.CASCADE, related_name='column')
    tags = TaggableManager(blank=True)
    cover = models.CharField(verbose_name='封面', max_length=255, null=True, blank=True)
    content = RichTextUploadingField(verbose_name='content', max_length=30000)
    is_public = models.SmallIntegerField(verbose_name='是否公开', choices=Is_Public_CHOICES, default=1)
    author = models.ForeignKey(verbose_name='发布人', to='account.UserInfo', on_delete=models.PROTECT)
    collectors = models.ManyToManyField('account.UserInfo', through='BlogCollector', related_name='collectors', verbose_name='收藏者')
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    # 获取文章地址
    def get_absolute_url(self):
        return reverse('blog_post', args=[self.id])

# 所有用户的收藏夹列表
class BlogFavorites(models.Model):
    title = models.CharField(verbose_name='收藏夹名称', max_length=50)
    intro = models.TextField(verbose_name='收藏夹简介', max_length=150)
    user = models.ForeignKey(verbose_name='收藏夹用户', to='account.UserInfo', on_delete=models.CASCADE)
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

class BlogCollector(models.Model):
    post = models.ForeignKey(verbose_name='收藏夹的文章', to='BlogPage', on_delete=models.CASCADE)
    user = models.ForeignKey(verbose_name='收藏夹用户', to='account.UserInfo', on_delete=models.CASCADE)
    favorite = models.ForeignKey(verbose_name='收藏夹', to='BlogFavorites', on_delete=models.CASCADE)
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)